import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import LoginMobile from '@/views/LoginMobile.vue'
import MainDashboard from '@/views/MainDashboard.vue'
import RefundTaskManagementMobile from '@/views/RefundTaskManagementMobile.vue'
import PatientContactQueryMobile from '@/views/PatientContactQueryMobile.vue'
import PatientCardInfoQueryMobile from '@/views/PatientCardInfoQueryMobile.vue'
import SoftwareLicenseQueryMobile from '@/views/SoftwareLicenseQueryMobile.vue'
import InpatientQueryMobile from '@/views/InpatientQueryMobile.vue'
import WeChatWorkLogin from '@/views/WeChatWorkLogin.vue'
import WeChatSuccess from '@/views/WeChatSuccess.vue'
import WeChatError from '@/views/WeChatError.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: LoginMobile,
    meta: { 
      requiresAuth: false,
      title: '登录'
    }
  },
  {
    path: '/login-mobile',
    name: 'LoginMobile',
    component: LoginMobile,
    meta: { 
      requiresAuth: false,
      title: '普通登录'
    }
  },
  {
    path: '/wechat-work-login',
    name: 'WeChatWorkLogin',
    component: WeChatWorkLogin,
    meta: { 
      requiresAuth: false,
      title: '企业微信登录'
    }
  },
  {
    path: '/wechat-success',
    name: 'WeChatSuccess',
    component: WeChatSuccess,
    meta: { 
      requiresAuth: false,
      title: '登录成功'
    }
  },
  {
    path: '/wechat-error',
    name: 'WeChatError',
    component: WeChatError,
    meta: { 
      requiresAuth: false,
      title: '登录失败'
    }
  },
  {
    path: '/',
    name: 'MainDashboard',
    component: MainDashboard,
    meta: { requiresAuth: true, title: '柳州市妇幼保健院信息系统' }
  },
  {
    path: '/refund-management',
    name: 'RefundTaskManagement',
    component: RefundTaskManagementMobile,
    meta: { requiresAuth: true, title: '退款任务管理' }
  },
  {
    path: '/workload-query',
    name: 'WorkloadQuery',
    component: () => import('@/views/RefundTaskWorkloadQuery.vue'),
    meta: { requiresAuth: true, title: '退费任务工作量查询' }
  },
  {
    path: '/patient-contact-query',
    name: 'PatientContactQuery',
    component: PatientContactQueryMobile,
    meta: { requiresAuth: true, title: '患者联系记录查询' }
  },
  {
    path: '/patient-card-query',
    name: 'PatientCardQuery',
    component: PatientCardInfoQueryMobile,
    meta: { requiresAuth: true, title: '患者卡信息查询' }
  },
  {
    path: '/software-license-query',
    name: 'SoftwareLicenseQuery',
    component: SoftwareLicenseQueryMobile,
    meta: { requiresAuth: true, title: '软件正版化查询' }
  },
  {
    path: '/inpatient-query',
    name: 'InpatientQuery',
    component: InpatientQueryMobile,
    meta: { requiresAuth: true, title: '住院查询' }
  },

]

const router = createRouter({
  history: createWebHistory('/refundtask/'),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  // 登录相关页面路径列表
  const loginPaths = ['/login', '/login-mobile', '/wechat-work-login', '/wechat-success', '/wechat-error']
  
  // 如果要去登录相关页面，直接放行
  if (loginPaths.includes(to.path)) {
    // 如果已经认证且要去登录页面，重定向到首页
    if (authStore.isAuthenticated && ['/login', '/login-mobile', '/wechat-work-login'].includes(to.path)) {
      next('/')
    } else {
      next()
    }
    return
  }
  
  // 如果需要认证但未认证，重定向到登录页面
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next('/login')
    return
  }
  
  // 患者卡信息查询权限控制
  if (to.path === '/patient-card-query') {
    const allowedUsernames = ["14019", "11638", "11110", "14052", "13638", "11409", "13646", "11754","13411", "14026", "14015", "13634", "14009", "11781", "13717", "14051","14166", "14164", "14050", "14025", "14022", "13632", "14039", "13637","11033", "13635", "14045", "11381", "14044", "13647", "13640", "11808","13641", "13644", "14030", "13643", "11377", "14049", "13639", "13649","14013", "11806", "11412", "14012", "13633", "11319", "13650", "14054","20182", "20184", "14011", "14014", "13645", "14017", "14165", "11563","14029", "14010", "13642", "13631", "13636", "14031", "11275", "20108","11136", "20214", "11692", "14149", "12700", "11611", "12082", "20232","20020", "13191", "11302", "13596", "13791", "11596", "20101", "13251","13432", "22432", "13223", "14147", "12084", "12123", "17002", "11411","11761", "13678", "13245", "22251", "12597", "12019", "13260", "11981","12495", "22232", "22606", "20008", "20179", "12442", "15040", "20180","20181", "22395", "20065", "11738", "20086", "13869", "11036", "13169","20160", "12121", "12314", "11431", "13593", "18011", "13105", "13046","20259", "11423", "13669", "14146", "20082", "11772", "13450", "11103","13570", "14364", "11205", "16020", "11777", "13046"]
    
    const currentUser = authStore.user
    if (!currentUser || !allowedUsernames.includes(currentUser.username)) {
      // 无权限访问，重定向到首页
      next('/')
      return
    }
  }
  
  next()
})

export default router